﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class frm_ThamSo : DevExpress.XtraEditors.XtraForm
    {
        #region Khai bao bien
        public int maThamSo=0;
        DataTable dt = new DataTable();
        TextMessageBox box = new TextMessageBox();
        ThamSo_BLL ts_bll = new ThamSo_BLL();
        private bool isOK = false;
        private int status;
        #endregion

        #region LoadForm

        public frm_ThamSo()
        {
            InitializeComponent();
        }

        private void frm_ThamSo_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region Cac chuc nang

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            updateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            updateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion

        #region cac su kien

        private void frm_ThamSo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }

        #endregion

        #region ham viet them
        private void updateData()
        {
            if (checkValid())
            {
                ThamSo_Public ts=new ThamSo_Public();
                if (maThamSo == 0)
                {
                    ts.TenThamSo = txtTenThamSo.Text;
                    ts.SoLuongMuon = box.ToInt(txtSoLuong.Text);
                    ts.ThoiGianMuon = box.ToInt(txtThoiGian.Text);
                    ts.Status = status;

                    ts_bll.Insert(ts);
                    CloesForm();
                }
                else
                {
                    ts.MaThamSo = maThamSo;
                    ts.TenThamSo = txtTenThamSo.Text;
                    ts.SoLuongMuon = box.ToInt(txtSoLuong.Text);
                    ts.ThoiGianMuon = box.ToInt(txtThoiGian.Text);
                    ts.Status = status;

                    ts_bll.Update(ts);
                    CloesForm();
                }
            }
        }
        private void CloesForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }
        private void LoadData()
        {
            if (maThamSo > 0)
            {
                dt = ts_bll.TaoBang(box.sqlCheckExist("ThamSo","MaThamSo",maThamSo));
                txtMa.Text = maThamSo.ToString();
                txtTenThamSo.Text = dt.Rows[0]["TenThamSo"].ToString();
                txtSoLuong.Text = dt.Rows[0]["SoLuongMuon"].ToString();
                txtThoiGian.Text = dt.Rows[0]["ThoiGianMuon"].ToString();
                int status1=box.ToInt(dt.Rows[0]["Status"]);
                if (status1 == 1)
                    ckbGiaoVien.Checked = true;
                else if(status1==0)
                    ckbHocVien.Checked = true;
                
            }
        }
        private bool checkValid()
        {
            if (txtTenThamSo.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên tham số.Hãy nhập lại.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenThamSo.Focus();
                return false;
            }
            if (txtSoLuong.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Số lượng cho mượn.Hãy nhập lại.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtSoLuong.Focus();
                return false;
            }
            if (txtThoiGian.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Số ngày mượn.Hãy nhập lại.", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtThoiGian.Focus();
                return false;
            }
            return true;
        }
        private void SetInterface()
        {
            txtMa.Text = "Mã tự sinh";
            txtTenThamSo.Text = "";
            txtSoLuong.Text = "";
            txtThoiGian.Text = "";
            maThamSo = 0;
        }
      
        #endregion

        #region cac su kien

        private void ckbHocVien_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbHocVien.Checked)
                status = 0;
        }

        private void ckbGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbGiaoVien.Checked)
                status = 1;
        }

        #endregion
    }
}